package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MapleStoryFrame;
import com.neutech.maplestory.util.MyMusicThread;
import sun.font.FontRunIterator;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/** （3）
 * 游戏的主入口文件
 * @author ZZQ
 * @PackageName: com.neutech.maplestory.client
 * @ClassName: MapleStoryClient
 * @Description:
 * @date 2021/1/27 13:13
 */
public class MapleStoryClient extends MapleStoryFrame {
    public Hero hero = new Hero(this,100,500);

    public List<Ground>grounds = new CopyOnWriteArrayList<>();

//    public List<Arrow>arrows = new ArrayList<>();
      public List<Arrow>arrows = new CopyOnWriteArrayList<>();//改为线程安全 消除因数组号改变而进行的图片闪烁(1)

//    public Mob mob = new Mob(this,700,550);

//    public List<Mob>mobs = new ArrayList<>();
      public List<Mob>mobs = new CopyOnWriteArrayList<>();//改为线程安全 消除因数组号改变而进行的图片闪烁(2)

//    public Random r = new Random();
    public List<Item> items = new CopyOnWriteArrayList<>();

    public List<Power> powers = new CopyOnWriteArrayList<>();

    public ItemPackage itemPackage = new ItemPackage(this);

    {
        for(int i = 0;i < 5;i ++){
            Mob mob = new Mob (this,380 + (i * 90),500,5);
            mobs.add(mob);
        }
        Mob mob1 = new Mob (this,500,230,5);
        mobs.add(mob1);
        Mob mob2 = new Mob (this,840,80,5);
        mobs.add(mob2);
        Mob mob3 = new Mob (this,180,80,5);
        mobs.add(mob3);
    }
    {
        Ground ground01 = new Ground(this,0,635,350,300);
        grounds.add(ground01);
        Ground ground02 = new Ground(this,350,685,315,100);
        grounds.add(ground02);
        Ground ground03 = new Ground(this,640,635,100,300);
        grounds.add(ground03);
        Ground ground04 = new Ground(this,740,585,700,400);
        grounds.add(ground04);
//        for (int i = 0; i < 5; i++) {
//            Ground g = new Ground(this,600 + (i * 200),500 - (i * 50),120,45);
//            grounds.add(g);
//        }
        Ground ground05 =new Ground(this,0,0,"ground4");
        grounds.add(ground05);
        Ground ground06 = new Ground(this,350,485,295,50);
        grounds.add(ground06);
        Ground ground07 = new Ground(this,245,150,345,50);
        grounds.add(ground07);
        Ground ground08 = new Ground(this,830,150,375,50);
        grounds.add(ground08);
        Ground ground09 = new Ground(this,570,320,305,50);
        grounds.add(ground09);


    }

//    public Arrow arrow = new Arrow(400,500, Direction.RIGHT);

    @Override
    public void init() {
        super.init();
        //添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.KeyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    //开关切换
                    itemPackage.live = !itemPackage.live;//按一下就是原来的相对面
//                    itemPackage.live = true;
                }
//                System.out.println("按下");

//                switch (e.getKeyCode()){
//                    case KeyEvent.VK_A:
//                        hero.x -= hero.speed;
//                        break;
//                    case KeyEvent.VK_W:
//                        hero.y -= hero.speed;
//                        break;
//                    case KeyEvent.VK_D:
//                        hero.x += hero.speed;
//                        break;
//                    case KeyEvent.VK_S:
//                        hero.y += hero.speed;
//                        break;
//                    default:
//                        break;
//                }

//                if(e.getKeyCode() == KeyEvent.VK_A ){
//                    System.out.println("按下");
//                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);

//                System.out.println("抬起");
//                if(e.getKeyCode() == KeyEvent.VK_S){
//                    System.out.println("抬起");
//                }

//                switch (e.getKeyCode()){
//                    case KeyEvent.VK_A:
//                        break;
//                    case KeyEvent.VK_W:
//                        break;
//                    case KeyEvent.VK_D:
//                        break;
//                    case KeyEvent.VK_S:
//                        break;
//                    default:
//                        break;
//                }
            }
        });
        new MyMusicThread("bgm.mp3", true).start();

        //背景音乐循环loop写true,不循环去掉true

        /**
         * 添加鼠标监听器
         */
        this.addMouseListener(new MouseListener() {
            //点击
            @Override
            public void mouseClicked(MouseEvent e) {

            }
            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    @Override
    public void paint(Graphics g) {
//        hero.draw(g);
//        arrow.draw(g);

//        hero.pickup(items);

        Iterator<Ground>itGrounds = grounds.iterator();
        while (itGrounds.hasNext()){
            Ground ground = itGrounds.next();
            ground.draw(g);

        }
        hero.draw(g);

        //怪物死亡闪烁未消除成功使用迭代器处理
        //使用迭代器和CopyOnWriterArrayList解决数组索引移动（非线程安全问题）
        Iterator<Arrow>itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
//        for (int i = 0; i < arrows.size(); i++) {
//            Arrow arrow = arrows.get(i);
//            arrow.draw(g);
//            arrow.hit(mobs);
//        }

//            arrows.get(i).draw(g);
        Iterator<Mob>itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
        }

        Iterator<Item>itItems = items.iterator();
        while (itItems.hasNext()){
            Item item = itItems.next();
            item.draw(g);
        }

        Iterator<Power> itpowers = powers.iterator();
        while (itpowers.hasNext()){
            Power power = itpowers.next();
            power.draw(g);
        }

        itemPackage.draw(g);

//        item.draw(g);

//        for (int i = 0; i < mobs.size(); i++) {
//            mobs.get(i).draw(g);
//
//        }

//        mob.draw(g);

        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("人物的方向为：" + hero.dir,100,100);
        g.drawString("人物的动作为：" + hero.action,100,150);
        g.drawString("人物趴下" + hero.down,100,200);
        g.drawString("count" + hero.count,100,250);
        g.drawString("shoot" + hero.shoot,100,300);
        g.drawString("arrows.size()" + arrows.size(),100,350);
        g.drawString("mobs.size()" + mobs.size(),100,400);
        g.drawString("人物的HP" + hero.HP,100,450);
        g.drawString("人物的MP" + hero.MP,100,500);

        g.setFont(f);
    }

    public static void main(String[]args){
        new MapleStoryClient().init();
    }
}


